﻿Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.IO
Imports SACommon
Imports SASettings

<ToolboxBitmap(GetType(SAUpdaterExec), "Icon_16_SA_UP_EXEC.png")>
Public Class SAUpdaterExec
    Dim uc1 As Control

    Private mRepositoryDefinition As String
    Private mServer As String
    Private mFolder As String
    Private mVersionFile As String
    Private mError As SAEventArgs

#Region "Events"
    Event InstallCompleted(Success As Boolean)
    Event InstallStarted(Success As Boolean)
#End Region
#Region "Class Definition"
    <Category("Repository")>
    <Description("Used to load a definition file created by SAUpdater to specify the various repository attributes.")>
    <Editor(GetType(UIFileEditor), GetType(UITypeEditor))>
    Public Property RepositoryDefinition As String
        Get
            Return mRepositoryDefinition
        End Get
        Set(value As String)
            Dim tString As String
            If value Is Nothing Then
                tString = ""
            Else
                tString = value
            End If

            If tString.Trim = "" Then
                RepositoryServer = ""
                RepositoryFolder = ""
                VersionFile = ""
                mRepositoryDefinition = ""
            ElseIf tString.Trim <> "" And Me.DesignMode = True Then
                RepositoryServer = UpdateCore.RepositoryServer
                RepositoryFolder = UpdateCore.RepositoryFolder
                VersionFile = UpdateCore.VersionFile
                mRepositoryDefinition = value
            Else
                mRepositoryDefinition = value
            End If
        End Set
    End Property
    <Category("Repository")>
    <Description("Sets the value of the repository server. eg http://www.contoso.com")>
    Public Property RepositoryServer As String
        Get
            Return UpdateCore.RepositoryServer
        End Get
        Set(value As String)
            Dim tString As String
            If value Is Nothing Then
                tString = ""
            Else
                tString = value
            End If

            mServer = tString
            UpdateCore.RepositoryServer = mServer
        End Set
    End Property
    <Category("Repository")>
    <Description("Sets the folder to be used on the repository server., eg /mySoftware/myApp")>
    Public Property RepositoryFolder As String
        Get
            Return UpdateCore.RepositoryFolder
        End Get
        Set(value As String)
            Dim tString As String
            If value Is Nothing Then
                tString = ""
            Else
                tString = value
            End If

            mFolder = tString
            UpdateCore.RepositoryFolder = mFolder
        End Set
    End Property
    <Category("Repository")>
   <Description("Sets the name for the Version file., eg Update.html")>
    Public Property VersionFile As String
        Get
            Return UpdateCore.VersionFile
        End Get
        Set(value As String)
            Dim tString As String
            If value Is Nothing Then
                tString = ""
            Else
                tString = value
            End If

            mVersionFile = tString
            UpdateCore.VersionFile = mVersionFile
            If mVersionFile.Trim <> "" Then SetDefaults()
        End Set
    End Property
    <Category("Application")>
    <Description("The title of the application, eg My Application")>
    Public Property ApplicationTitle As String
        Get
            Return MyApp.ApplicationTitle
        End Get
        Set(value As String)
            MyApp.ApplicationTitle = value
        End Set
    End Property
    <Category("Application")>
<Description("The title of the update, eg My Updater")>
    Public Property UpdaterTitle As String
        Get
            Return MyApp.UpdaterTitle
        End Get
        Set(value As String)
            MyApp.UpdaterTitle = value
        End Set
    End Property
#Region "Non Browsable"
    <Browsable(False)>
    Public Property CurrentVersion As String
        Get
            Return UpdateCore.CurrentAppVersion
        End Get
        Set(value As String)
            UpdateCore.CurrentAppVersion = value
        End Set
    End Property
    <Browsable(False)>
    Public Property AllowBeta As Boolean
        Get
            Return MyApp.AllowBeta
        End Get
        Set(value As Boolean)
            MyApp.AllowBeta = value
        End Set
    End Property
    <Browsable(False)>
    Public Property IsUpdate As Boolean
        Get
            Return MyApp.IsUpdate
        End Get
        Set(value As Boolean)
            MyApp.IsUpdate = value
        End Set
    End Property
    Function ProcessCommandLineArguments(CommandLineArguments As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
        Dim tValid As Boolean = False
        If CommandLineArguments.Count > 0 Then
            For Each item In CommandLineArguments
                If InStr(item.ToUpper, "/UPDATE") > 0 Then IsUpdate = True
                If InStr(item.ToUpper, "/BETA") > 0 Then AllowBeta = True
                If InStr(item.ToUpper, "/V") > 0 Then
                    tValid = True
                    CurrentVersion = Strings.Right(item.Trim, Len(item.Trim) - 2).Trim
                End If
            Next
        Else
            IsUpdate = False
            CurrentVersion = "0.0.0.0"
            AllowBeta = False
        End If
        Return tValid
    End Function
    Sub AddNewSetting(Key As Integer, Value As Object)
        gNewSettings.Add(Key, Value)
    End Sub
    Sub ShowBlankScreen()
        ControlLoadState()
        uc1 = New ucBlank
        DisplayControl(uc1)
    End Sub
#End Region
#End Region
#Region "Get Properties"
    ReadOnly Property UpdaterVersion As String
        Get
            Return Application.ProductVersion
        End Get
    End Property
#End Region
    Private Sub SAUpdaterExec_Load(sender As Object, e As EventArgs) Handles Me.Load
        MyApp.DesignMode = Me.DesignMode

        If MyApp.DesignMode = False Then
            gApp = New MyApp
            gGenInfo = New GeneralInfo
            gUpdateVersion = New UpdateVersion
            gUpdateSettings = New UpdateSettings
            gUpdateFiles = New UpdateFiles
            gNewSettings = New Settings("")

            UpdateCore.CurrentAppVersion = CurrentVersion

            AddHandler Messenger.MessengerMessage, AddressOf OnTaskMessage
            DisplayStartControl()
        Else
            DisplayDesignControl()
        End If
    End Sub

#Region "Check Update"
    Function SetDefaults() As Boolean
        If ApplicationTitle Is Nothing Then ApplicationTitle = "My Application"
        If ApplicationTitle.Trim = "" Then ApplicationTitle = "My Application"

        If UpdaterTitle Is Nothing Then UpdaterTitle = "My Updater"
        If UpdaterTitle.Trim = "" Then UpdaterTitle = "My Updater"

        If CurrentVersion Is Nothing Then CurrentVersion = "0.0.0.0"
        If CurrentVersion.Trim = "" Then CurrentVersion = "0.0.0.0"

        If IsUpdate = Nothing Then IsUpdate = False

        UpdateCore.RepositoryServer = RepositoryServer
        UpdateCore.RepositoryFolder = RepositoryFolder
        UpdateCore.VersionFile = VersionFile

        If UpdateCore.RepositoryServer.Trim = "" Then
            SetErrorEncounteredState("Illegal Server Repository", SAEventArgs.Results.MissingServerName)
            Return False
        End If

        If UpdateCore.RepositoryFolder.Trim = "" Then
            SetErrorEncounteredState("Illegal Repository Folder", SAEventArgs.Results.MissingFolder)
            Return False
        End If

        If UpdateCore.VersionFile.Trim = "" Then
            SetErrorEncounteredState("Illegal Version File", SAEventArgs.Results.MissingVersionFile)
            Return False
        End If

        gUpdateFiles.AppDirectory = UpdateCore.FixFolderText(String.Format("{0}\{1}", Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.Personal)).FullName, ApplicationTitle))

        Return True
    End Function
#End Region
#Region "Display Controls"
    Sub DisplayDesignControl()
        ControlLoadState()
        uc1 = New ucDesigner
        DisplayControl(uc1)
    End Sub
    Sub DisplayStartControl()
        ControlLoadState()
        uc1 = New ucStart
        DisplayControl(uc1)
    End Sub
    Sub DisplayDownloadControl()
        ControlLoadState()
        uc1 = New ucDownload
        DisplayControl(uc1)
    End Sub
    Sub DisplayDownloadFilesControl()
        ControlLoadState()
        uc1 = New ucDownloadFiles
        DisplayControl(uc1)
    End Sub
    Sub DisplayInstallControl()
        ControlLoadState()
        uc1 = New ucInstall
        DisplayControl(uc1)
    End Sub
    Sub DisplaySQLInstallControl()
        ControlLoadState()
        uc1 = New ucSQLInstall
        DisplayControl(uc1)
    End Sub
    Sub DisplayEndControl(Success As Boolean)
        ControlLoadState()
        uc1 = New ucEnd(Success)
        DisplayControl(uc1)
    End Sub
#End Region
#Region "Control"
    Sub ControlLoadState()
        Dim s As String
        Me.Cursor = Cursors.WaitCursor
        For Each Control In clientPanel.Controls
            Dim x = Control.GetType
            Try
                If Strings.Left(x.Name, 2) = "uc" Then Control.dispose()
            Catch ex As Exception
                s = ex.Message
            End Try
        Next
        clientPanel.Controls.Clear()
        Me.Enabled = False
    End Sub
    Sub DisplayControl(uc As Control)
        clientPanel.Controls.Add(uc)
        uc.BringToFront()
        uc.Dock = DockStyle.Fill
        Me.Cursor = Cursors.Default
        Me.Enabled = True
    End Sub
#End Region
    Sub SetErrorEncounteredState(Description As String, Result As SAEventArgs.Results)
        'gTimer.Stop()
        mError = New SAEventArgs(Description, Result)
    End Sub
    Sub OnTaskMessage(MessageType As Messenger.MessageType, MessageContent As Messenger.MessageContent)
        Dim tContent As String = MessageContent.Message
        Dim tSuccess As Boolean = MessageContent.Success

        Select Case MessageType
            Case Is = Messenger.MessageType.ChangePage
                Select Case tContent
                    Case Is = MyApp.ApplicationType.StartApplication
                        DisplayStartControl()
                    Case Is = MyApp.ApplicationType.Download
                        DisplayDownloadControl()
                    Case Is = MyApp.ApplicationType.DownloadFiles
                        DisplayDownloadFilesControl()
                    Case Is = MyApp.ApplicationType.Install
                        DisplayInstallControl()
                    Case Is = MyApp.ApplicationType.InstallEnd
                        DisplayEndControl(tSuccess)
                    Case Is = MyApp.ApplicationType.EndApplication
                        RaiseEvent InstallCompleted(tSuccess)
                End Select
            Case Is = Messenger.MessageType.AppEvent
                Select Case tContent
                    Case Is = "InstallStarted"
                        RaiseEvent InstallStarted(tSuccess)
                End Select
        End Select
    End Sub
End Class
